#include "store.h"
#include "norflash.h"
#include "usart.h"
#include "SEGGER_RTT.h"

#define EFFECTIVE_FLAG 0xaa55bb66

osMutexId_t w25q64Mutex;

void reInitByStore(){
    uint32_t tmp;
    norflash_read((uint8_t*)&tmp,0,4);
    if(tmp != EFFECTIVE_FLAG){
        /* 无效标志位,初始化 */
        //写入有效标志位
        norflash_erase_sector(0);
        tmp = EFFECTIVE_FLAG;
        norflash_write((uint8_t*)(&tmp),0,4);

        //写入默认波特率
        uint32_t baudrate[2] = {huart1.Init.BaudRate, huart2.Init.BaudRate};
        norflash_write((uint8_t*)baudrate,BAUDRATE_ADDR,8);

        SEGGER_RTT_WriteString(0,"写入默认波特率\r\n");
        norflash_read((uint8_t*)&tmp,0,4);
    }
    else{
        /* 有效标志位,读取波特率 */
        norflash_read((uint8_t*)&tmp,BAUDRATE_ADDR,4);
        huart1.Init.BaudRate = tmp;
        HAL_UART_Init(&huart1);
        norflash_read((uint8_t*)&tmp,BAUDRATE_ADDR+4,4);
        huart2.Init.BaudRate = tmp;
        HAL_UART_Init(&huart2);

        SEGGER_RTT_WriteString(0,"读取波特率\r\n");
    }
    //创建存储芯片互斥锁
    w25q64Mutex = osMutexNew(NULL);
}